import { Component, OnInit, Input, OnDestroy, HostListener } from "@angular/core";
import { OrderManageService } from "src/app/@core/order-manage.service";
import { AuthGuardService } from "src/app/@core/auth-guard-service.guard";

@Component({
  selector: "app-qr-code",
  templateUrl: "./qr-code.component.html",
  styleUrls: ["./qr-code.component.scss"],
})
export class QrCodeComponent implements OnInit, OnDestroy {
  @HostListener('window: onload', ['$event']) onload(event: Event) {
    this.cancel.unsubscribe()
  }
  @Input() data: any;
  cancel: any

  constructor(private authGuardService: AuthGuardService, private orderManageService: OrderManageService) { }

  ngOnInit(): void {
    this.cancel = this.orderManageService.buildWebsocket(this.data.orderId).subscribe((res) => {
      if (res.code === 0 && res.data.orderStatus === 'SUCCESS') {
        this.authGuardService.showModalPanel('success', res.msg)
        this.data.close();
      }
      else {
        this.authGuardService.showModalPanel('warning', res.msg)
        this.data.close();
      }
    })
  }

  ngOnDestroy() {
    this.cancel.unsubscribe()
  }
}
